Completed
Push — master ( b17c31...9a771d )
by greg
01:53
created

Page.js ➔ ???   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
nc 10
nop 4
dl 0
loc 66
cc 7
rs 7.0832

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import Handlebars from 'handlebars'
2
import path from 'path'
3
import fse from 'fs-extra'
4
5
import {
6
  abeEngine,
7
  cmsData,
8
  cmsTemplates,
9
  config,
10
  abeExtend,
11
  Manager
12
} from '../'
13
14
/**
15
 * Page class
16
 * manage HTML generation for page template
17
 */
18
export default class Page {
19
20
  /**
21
   * Create new page object
22
   * @param  {Object} params req.params from express route
0 ignored issues
show
Documentation introduced by
The parameter params does not exist. Did you maybe forget to remove this comment?
Loading history...
23
   * @param  {Object} i18n translation
0 ignored issues
show
Documentation introduced by
The parameter i18n does not exist. Did you maybe forget to remove this comment?
Loading history...
24
   * @param  {Function} callback 
0 ignored issues
show
Documentation introduced by
The parameter callback does not exist. Did you maybe forget to remove this comment?
Loading history...
25
   * @param  {Boolean} onlyHTML default = false, if true HTML content will contains abe attributes
26
   * @return {String} HTML page as string
27
   */
28
  constructor(templateId, template, json, onlyHTML = false) {
29
    // HOOKS beforePageJson
30
    json = abeExtend.hooks.instance.trigger('beforePageJson', json)
31
32
    abeEngine.instance.content = json
33
      
34
    if(typeof Handlebars.templates[templateId] !== 'undefined' && 
35
        Handlebars.templates[templateId] !== null && 
36
        config.files.templates.precompile
37
    ){
38
      template = Handlebars.templates[templateId]
39
      this.html = template(json, {data: {intl: config.intlData}})
40
41
      //console.log('precompile')
42
43
    } else {
44
45
      this._onlyHTML = onlyHTML
46
      this.template = template
47
      this.HbsTemplatePath = path.join(config.root, config.templates.url, 'hbs/'+templateId+'.hbs')
48
49
      // Remove text with attribute "visible=false"
50
      this.template = cmsTemplates.prepare.removeHiddenAbeTag(this.template)
51
52
      if(!this._onlyHTML){
53
        // Surrounds each Abe tag (which are text/rich/textarea and not in html attribute) with <abe> tag
54
        // ie. <title><abe>{{abe type='text' key='meta_title' desc='Meta title' tab='Meta' order='4000'}}</abe></title>
55
        this.template = cmsTemplates.prepare.addAbeHtmlTagBetweenAbeTags(this.template)
56
57
        this.template = cmsTemplates.prepare.addAbeDataAttrForHtmlAttributes(this.template)
58
59
        this.template = cmsTemplates.prepare.addAbeDataAttrForHtmlTag(this.template)
60
61
        this.template = cmsTemplates.prepare.addAbeSourceComment(this.template, json)
62
      } else {
63
        this.template = cmsTemplates.prepare.removeHandlebarsRawFromHtml(this.template)
64
      }
65
66
      // je rajoute les index pour chaque bloc lié à un each
67
      this.template = cmsTemplates.prepare.indexEachBlocks(this.template, this._onlyHTML)
68
69
      // We remove the {{abe type=data ...}} from the text 
70
      this.template = cmsData.source.removeDataList(this.template)
71
72
      // It's time to replace the [index] by {{@index}} (concerning each blocks)
73
      this.template = cmsTemplates.prepare.replaceAbeEachIndex(this.template)
74
75
      if(config.files.templates.precompile){
76
        // Let's persist the precompiled template for future use (kind of cache)
77
        fse.writeFileSync(this.HbsTemplatePath, Handlebars.precompile(this.template), 'utf8')
78
        Manager.instance.addHbsTemplate(templateId)
79
      }
80
81
      // I compile the text
82
      var compiledTemplate = Handlebars.compile(cmsTemplates.insertDebugtoolUtilities(this.template, this._onlyHTML))
83
84
      // I create the html page ! yeah !!!
85
      this.html = compiledTemplate(json, {data: {intl: config.intlData}})
86
    }
87
88
    if(this._onlyHTML) {
89
      this.html = abeExtend.hooks.instance.trigger('afterPageSaveCompile', this.html, json)
90
    }else {
91
      this.html = abeExtend.hooks.instance.trigger('afterPageEditorCompile', this.html, json)
92
    }
93
  }
94
}